package com.t4.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t4.auth.service.SysRoleService;
import com.t4.common.result.Result;
import com.t4.model.system.SysRole;
import com.t4.vo.system.AssignRoleVo;
import com.t4.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * id查询角色
     * @param id
     * @return
     */
    @ApiOperation(value = "id查询角色")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable Long id){
        SysRole sysRole = sysRoleService.getById(id);
        return Result.success(sysRole);
    }


    /**
     * 获取全部角色列表
     * @return
     */
    @ApiOperation(value = "获取全部角色列表")
    @GetMapping("/getAll")
    public Result getAll(){
        List<SysRole> list = sysRoleService.list();
        return Result.success(list);
    }


    /**
     * 条件分页查询
     * @param current 当前页码
     * @param size 每页显示记录数
     * @param sysRoleQueryVo 查询的条件对象
     * @return
     */
    @ApiOperation(value = "角色条件分页查询")
    @GetMapping("/{current}/{size}")
    public Result pageQueryRole(@PathVariable Long current,
                                @PathVariable Long size,
                                SysRoleQueryVo sysRoleQueryVo){
        // 1. 创建Page对象，传递参数
        Page<SysRole> pageParam = new Page<>(current, size);
        // 2. 封装条件，判断是否为空，不为空则进行封装
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if(!StringUtils.isEmpty(roleName)){
            queryWrapper.like(SysRole::getRoleName, roleName);
        }
        // 3. 调用方法实现
        sysRoleService.page(pageParam, queryWrapper);
        return Result.success(pageParam);
    }

    /**
     * 添加角色
     * @param sysRole 角色对象
     * @return
     */
    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole){
        boolean is_success = sysRoleService.save(sysRole);
        if(is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }


    /**
     * 修改角色
     * @param sysRole
     * @return
     */
    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    public Result updateById(@RequestBody SysRole sysRole){
        boolean is_success = sysRoleService.updateById(sysRole);
        if(is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }


    /**
     * 删除角色
     * @param id
     * @return
     */
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id){
        boolean is_success = sysRoleService.removeById(id);
        if(is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }

    /**
     * 批量删除角色
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除角色")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        boolean is_success = sysRoleService.removeByIds(ids);
        if(is_success){
            return Result.success();
        }else {
            return Result.fail();
        }
    }


    @ApiOperation(value = "根据用户获取角色信息")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId){
        HashMap<String, Object> map = sysRoleService.toAssign(userId);
        return Result.success(map);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignRoleVo assginRoleVo){
        sysRoleService.doAssign(assginRoleVo);
        return Result.success();
    }
}
